var fetch = require('@system.fetch');

var API_ROOT = 'http://api.avatardata.cn/';
var headers = {
	'Content-Type': 'application/x-www-form-urlencoded'
};
var key = '5b8eb3a694f84ac9934dd47c0b6f3f0b';
import prompt from '@system.prompt'
function getTimestamp() {
	let nowDate = new Date(); 
	let hours = nowDate.getHours();
	let minutes = nowDate.getMinutes();
	let seconds = nowDate.getSeconds();
	if (hours < 10) {
		hours = '0' + String(hours);
	}
	if (minutes < 10) {
		minutes = '0' + String(minutes);
	}
	if (seconds < 10) {
		seconds = '0' + String(seconds);
	}
	return String(hours) + String(minutes) + String(seconds);
}

function realFetch(url, data = null, method = 'get') {
	if (data == null) {
		data = {};
	}
	return new Promise((resolve, reject) => {
		fetch.fetch({
			url: API_ROOT + url,
			data: data,
			header: headers,
			method: method,
			success: function(data) {
				resolve(data);
			},
			fail: function(data, code) {
			    console.log(data);
                prompt.showDialog({
                    title: '请求出错！',
                    message: '请求出错，请稍后重新再试！'
                });
				// reject(data, code);
			}
		});
	});
}

function post(url, data = null) {
	return realFetch(url, data, 'post');
}

function get(url, data = null) {
	return realFetch(url, data, 'get');
}
 
export  function getTangShiList(keyWord,page,limit) {
		return get('TangShiSongCi/Search',{
            key:key,
            keyWord:keyWord,
            page:page,
            rows:limit
		}).then((response) => {
            return Promise.resolve(JSON.parse(response.data));
        }).catch((err) => {
            return Promise.reject(err);
	})
}
function getDate() {
    let nowDate = new Date();
    let year = nowDate.getFullYear()+'';
    let month = nowDate.getMonth()+1+''
    let day = nowDate.getDate()+''
    return year+month+day;
}
export  function getPingdaoYugao(id) {
		return post('/1637-2',{
            key:key,
            id:id,
		}).then((response) => {
            return Promise.resolve(JSON.parse(response.data));
        }).catch((err) => {
            return Promise.reject(err);
	})
}

export  function getTangShiInfo(id) {
    return get('TangShiSongCi/LookUp',{
        key:key,
        id:id,
    }).then((response) => {
        return Promise.resolve(JSON.parse(response.data));
    }).catch((err) => {
        return Promise.reject(err);
    })
}


export  function loc() {
    return new Promise((resolve, reject) => {
        fetch.fetch({
            url: 'http://dh38922.com/switch/api/get_loc',
            header: headers,
            method: 'get',
            success: function(response) {
                resolve(JSON.parse(response.data));
            },
            fail: function(data, code) {
                reject(data, code);
            }
        });
    });
}